/*
 * Project Name: jmax
 * File Name: SocketClient.java
 * Copyright: Copyright(C) 1985-2015 ZKTeco Inc. All rights reserved.
 */
package com.xdtech.jmax.test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.charset.Charset;

import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.filter.logging.LoggingFilter;
import org.apache.mina.transport.socket.nio.NioSocketConnector;

import com.xdtech.socket.server.SocketUtil;

/**
 * TODO 一句话功能简述，请确保和下面的block tags之间保留一行空行
 * <p>
 * TODO 功能详细描述，若不需要请连同上面的p标签一起删除
 * 
 * @author <a href="max.zheng@zkteco.com">郑志雄</>
 * @version TODO 添加版本
 * @see 相关类或方法，不需要请删除此行
 * @since 2015-4-2 下午3:59:28
 */
public class SocketClient
{
	Socket socket;
	BufferedReader in;
	PrintWriter out;

	public SocketClient(String sn,String ip)
	{
		try
		{
			socket = new Socket("127.0.0.1", 9001);
//			socket = new Socket("192.168.1.101", 9000);
//			Socket socket = new Socket();
//			socket.connect(new InetSocketAddress("192.168.0.28", 9001), 3500);
//			socket.connect(new InetSocketAddress("192.168.1.101", 9000), 3500);
//			socket.setSoTimeout(2000);
			
//			in = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8"));
			out = new PrintWriter(socket.getOutputStream(), true);
			//            BufferedReader line = new BufferedReader(new InputStreamReader(System.in));  

		
			out.println("{\"SN\": \"123456789\",\"commit\": [{\"ac\": [{\"addr\": \"1\",\"channel\": \"1\",\"fan\": \"low\",\"mode\": \"auto\",\"power\":\"on\",\"temp\": \"13\"}]}]}");
			
			
			
//			out.println("{\"SN\":\"123456789\",\"commit\":[{\"dimmer\":[{\"addr\": \"4\",\"channel\":\"0\",\"status\": \"on\",\"freq\":\"0\",\"delay\":\"22\",\"value\": \"0\" }]}]}");
//			            out.println("{\"SN\":\"123456789\",\"commit\":[{\"fault\":[{\"addr\": \"2\",\"channel\":\"0\",\"type\": \"0\"}]}]}"); 
//			            out.println("{\"SN\":\"123456789\",\"commit\":[{\"scene\":[{\"number\": \"1\"}]}]}");
			//顾客
//			out.println("{\"SN\":\""+sn+"\",\"commit\":[{\"room\":[{\"number\": \"15\"}]}]}"); 
			//            line.close();  
			//节点
//			out.println("{\"SN\":\"123456789\",\"commit\":[{\"node\":[{\"addr\":\"6\",\"channel\":\"6\",\"status\":\"off\",\"freq\":\"0\",\"delay\":\"0\"}]}]}"); 
//			{"addr": "0","channel": "0","type": "0"},
			//故障
//			out.println("{\"SN\":\"123456789\",\"commit\":[{\"fault\": [{\"addr\" : \"7\",\"channel\" : \"7\",\"type\": \"0\"}]}]}");
			System.out.println("服务端返回" + SocketUtil.readStrFromStream(socket
					.getInputStream()));
//			out.close();
//			in.close();

//			socket.close();

		}
		catch (IOException e)
		{
			e.printStackTrace();
		}

	}

	public static void main(String[] args) throws Exception   
    {  
		new SocketClient("123456789","1"); 
//		for (int i = 0; i < 250; i++) {
//			final int j = i;
//			new Thread(new Runnable() {
//				
//				@Override
//				public void run() {
//					while(true) {
//						String sn = (35111111+j)+"";
//						new SocketClient(sn,j+""); 
//						try {
//							Thread.sleep(2000);
//						} catch (InterruptedException e) {
//							// TODO Auto-generated catch block
//							e.printStackTrace();
//						}
//					}
//					
//				}
//			}, i+"").start();
//		}
         
//    	 for(int i=0;i<1;i++){
//         	Thread.sleep(300);
//         	System.out.println(i);
//         	Thread t=new Thread(new A(String.valueOf(i)));
//         	t.start();
//         }
		 // 创建客户端连接器. 
//		  NioSocketConnector connector = new NioSocketConnector(); 
//		  connector.getFilterChain().addLast( "logger", new LoggingFilter() ); 
//		  connector.getFilterChain().addLast( "codec", new ProtocolCodecFilter( new TextLineCodecFactory( Charset.forName( "GBK" )))); //设置编码过滤器 
//		  connector.setConnectTimeout(1); 
//		  connector.setHandler(new MinaHandler());//设置事件处理器 
//		  ConnectFuture cf = connector.connect( 
//		  new InetSocketAddress("127.0.0.1", 8899));//建立连接 
//		  cf.awaitUninterruptibly();//等待连接创建完成 
//		  cf.getSession().write("知识");//发送消息 
//		  //cf.getSession().write("quit");//发送消息 
//		  cf.getSession().close();
//		  cf.getSession().getCloseFuture().awaitUninterruptibly();//等待连接断开 
//		  connector.dispose();
    }
}

class A implements Runnable
{

	public static final String IP_ADDR = "127.0.0.1";// 服务器地址
	public static final int PORT = 8899;// 服务器端口号
	String s = "";

	public A(String st)
	{
		s = st;
	}

	@Override
	public void run()
	{
		// TODO Auto-generated method stub
		Socket socket = null;
		try
		{
			socket = new Socket(IP_ADDR, PORT);

			PrintStream out = new PrintStream(socket.getOutputStream());
			while (true)
			{
				out.print("welcome to mina "+s);
				//String ret = input.readUTF();
				//System.out.println("服务器端返回过来的是: " + ret);
				//out.close();
				Thread.sleep(3000);

			}

		}
		catch (UnknownHostException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}
